﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QuanLyBanVeXe
{
    public partial class FormDoiMatKhau : Form
    {
        public static string Username = string.Empty;
        public FormDoiMatKhau()
        {
            InitializeComponent();
        }
        public SqlConnection myConnection;
        public SqlCommand myCommand;
        private DataTable dt = new DataTable();
        private SqlDataAdapter da = new SqlDataAdapter();

        public void KetNoi()
        {
            string strconnect = @"server=.\SQLEXPRESS; database=QuanLyBanVeXe; integrated security=true";
            myConnection = new SqlConnection(strconnect);
            myCommand = myConnection.CreateCommand();
        }
        private void FormDoiMatKhau_Load(object sender, EventArgs e)
        {
            KetNoi();
            this.AcceptButton = btDongY;
            if (!string.IsNullOrEmpty(Username))
                this.txtTenTaiKhoan.Text = Username;
        }
        public bool KiemTraMatKhau()
        {

            myCommand = new SqlCommand();
            myCommand.Connection = myConnection;
            myCommand.CommandType = CommandType.Text;
            myCommand.CommandText = @"select * from TaiKhoan
                                            where(TenTK=@TenTK)";
            myCommand.Parameters.Add("@TenTK", SqlDbType.NVarChar, 50).Value = txtTenTaiKhoan.Text;
            da.SelectCommand = myCommand;
            da.Fill(dt);
            bool kq = true;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i][1].ToString().CompareTo(txtMatKhauCu.Text) == 0)
                {
                    kq = false;
                    break;
                }

            }
            return kq;
        }
        private void btDongY_Click(object sender, EventArgs e)
        {
            if (txtMatKhauCu.Text.Trim().Length != 0)
            {
                if (KiemTraMatKhau())
                {
                    lblDoiMatKhau.Text = "Mật khẩu cũ không đúng!";
                    txtMatKhauCu.Focus();
                }
                else

                    if (txtMatKhauMoi.Text.Trim().Length == 0)
                    {
                        lblDoiMatKhau.Text = "Nhập mật khẩu mới!";
                        this.txtMatKhauMoi.Focus();
                    }
                    else
                    {
                        if (txtXacNhanMatKhauMoi.Text.Trim().Length == 0)
                        {
                            lblDoiMatKhau.Text = "Nhập lại mật khẩu mới!";
                            this.txtXacNhanMatKhauMoi.Focus();

                        }
                        else
                            if (txtMatKhauMoi.Text.CompareTo(txtXacNhanMatKhauMoi.Text) != 0)
                                lblDoiMatKhau.Text = "Mật khẩu mới không trùng nhau";
                            else
                            {
                                try
                                {
                                if (this.myConnection.State == ConnectionState.Closed)
                                    this.myConnection.Open();
                                myCommand.CommandType = CommandType.Text;
                                myCommand.CommandText = "Update TaiKhoan set MatKhau='" + txtMatKhauMoi.Text + "' where TenTK='" + txtTenTaiKhoan.Text + "'";
                                myCommand.ExecuteNonQuery();
                                myConnection.Close();
                                MessageBox.Show("Đổi Mật khẩu thành công");
                                this.Close();
                                }
                                catch { MessageBox.Show("Đổi mật khẩu không thành công!"); }
                            }
                    }
            }
            else
            {
                lblDoiMatKhau.Text = "Nhập mật khẩu cũ!";
                this.txtMatKhauCu.Focus();
            }
        }

        private void btHuyBo_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
